package ar.edu.unicen.pladema.vc1.formats.hgt;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;

public class HGT {
	private HashMap<String, Integer> matrix;
	private int minValue;
	private int maxValue;
	public Integer getValue(int i, int j) {
		return matrix.get(i + "@" + j);
	}
	private String getKey(int i) {
		return (i/600) + "@" + (i%600);
	}
	private void fillMatrix(Integer init) {
		for (int i=0; i<600; i++) {
			for (int j=0; j<600; j++) {
				matrix.put(j + "@" + i, init);
			}
		}
	}
	public static int unsignedByteToInt(byte b) {
	    return (int) b & 0xFF;
	    }
	
	public void loadFromFile(String filep) {
		matrix = new HashMap<String, Integer>();
		fillMatrix(new Integer(-1));
		InputStream hgt = null;
		byte[] step = new byte [2];
		try {
			hgt = new FileInputStream(filep);
			int i = 0;
			minValue = Integer.MAX_VALUE;
			maxValue = Integer.MIN_VALUE;
			int lastHeight=0;
			while (hgt.read(step) == 2) {
				//char[] chars [j] = (char) (bytes [i] + bytes [i+1]);
				//char c = (char) (step[0] + step[1]);
				//Integer altura = new Integer ((int) (c));
				int altura2 = 0;
				int b0 = unsignedByteToInt(step[0]);
				int b1 = unsignedByteToInt(step[1]);
				if (b1 != 0) {
					System.out.println(i + ": cambio abrupto:" + b1);
				}
				altura2 += b1 << 8;
				altura2 += b0;
				System.out.println(i + ": " + altura2 + " aa " + b0 + 
						":" + b1);
				if (lastHeight != 0) {
					if (Math.abs(lastHeight - altura2)> 900) {
						System.out.println("cambio abrupto");
					}
				}
				lastHeight = altura2;
				String key = getKey(i);
				matrix.put(key, altura2);
				i++;
				if (minValue > altura2) {
					System.out.println("cambiando menor de " + minValue + " a " + altura2);
					minValue = altura2;
				}
				if (maxValue < altura2) {
					System.out.println("\t\t\t\t\t\tcambiando mayor de " + maxValue + " a " + altura2);
					maxValue = altura2;
				}
			}
			System.out.println(minValue +" y "+ maxValue + ". "+ i);
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			if (hgt !=null) {
				try {hgt.close();} catch(Exception e) {}
			}
		}
	}
	public boolean loaded() {
		return (matrix != null);
	}
	/**
	 * @return the minValue
	 */
	public int getMinValue() {
		return minValue;
	}
	/**
	 * @return the maxValue
	 */
	public int getMaxValue() {
		return maxValue;
	}
	public void printMatrix() {
		System.out.println(matrix.size());
		for (int i=0; i<600; i++) {
			for (int j=0; j<600; j++) {
				System.out.print(getValue(i,j));
				System.out.print(" | ");
			}
			System.out.println(" ]");
		}
	}
	public static void main(final String [] args) {
		HGT hgt = new HGT();
		hgt.loadFromFile("/home/apps/desarrollo/workspace-swt/Visualizacion/resources/HGT/S38W060.M01");
		System.out.println(" 599 " + hgt.getKey(599));
		System.out.println(" 600 " + hgt.getKey(600));
		System.out.println(" 601 " + hgt.getKey(601));
		//hgt.printMatrix();
	}
}
